layui.use(['layer','upload','layedit','treeSelect','table','form','laytpl','element'], function(){
    var $ = layui.$
        ,layer = layui.layer
        ,admin = layui.admin
        ,table = layui.table
        ,element = layui.element
        ,form = layui.form
        ,laytpl = layui.laytpl;

    //获取路径参数
    var request = GetRequest();

    //设备表格名称
    $.get('/table/' + request.id,function (data) {
        $("#id").val(data.id);
        $("#name").val(data.name);
        $("#nameZh").val(data.nameZh);
        $("#remarks").val(data.remarks);

        var option = {
            elem: '#tableList',
            url : '/table/rows/list/' + data.id,
            cellMinWidth : 95,
            page : true,
            height : "full-125",
            limits : [10,15,20,25],
            limit : 15,
            id : "listTable",
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'name', title: '字段名', minWidth:100, align:"center"},
                {field: 'type', title: '字段类型', minWidth:100, align:"center"},
                {field: 'nameZh', title: '名称', minWidth:100, align:"center"},
                {field: 'formType', title: '表单类型', minWidth:100, align:"center"},
                {title: '操作', minWidth:150, fixed:"right",align:"center",templet: function(d){
                        var addBtn='<a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>';
                        var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                        return addBtn + delBtn;
                    }}
            ]]
        };

        //表格类型列表
        table.render(option);
    });

    //追加新的商品规格项
    var i = 0;
    $("#add-spec-item").click(function () {
        i++;
        var innerHtml = '<div style="margin-top: 10px;" class="layui-input-block">' +
            '                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ i +'].name" id="item-name" lay-verify="required" placeholder="字段名" autocomplete="off" class="layui-input" value="">' +
            '                </div>' +
            '                <div class="layui-form-mid">-</div>' +
            '                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ i +'].type" id="type" placeholder="字段类型" autocomplete="off" class="layui-input price" value="">' +
            '                </div>' +
            '                <div class="layui-form-mid">-</div>' +
            '                <div class="layui-input-inline">' +
            '                   <input type="text" name="tableRows['+ i +'].nameZh" id="price" placeholder="中文名称" autocomplete="off" class="layui-input stock" value="">' +
            '                </div>' +
            '                <div class="layui-input-inline">' +
            '                   <select id="" name="tableRows['+ i +'].formType" id="formType" lay-verify="required">' +
            '                       <option value="">表单类型</option>' +
            '                       <option value="input">输入框</option>' +
            '                       <option value="textarea">文本输入框</option>' +
            '                       <option value="date">时间</option>' +
            '                       <option value="img">图片</option>' +
            '                       <option value="layedit">富文本编辑器</option>' +
            '                   </select>' +
            '                </div>' +
            '            </div>'+
            '            <button style="position: relative;top: -37px;" id="del-spec-item'+ (i) +'" class="layui-btn layui-btn-danger"><i class="layui-icon layui-icon-delete"></i>删除</button>';
        $(this).before(innerHtml);
        form.render('select');
        //绑定事件
        $("#del-spec-item" + i).click(function () {
            $(this).prev().remove();
            $(this).remove();
            return false;
        });
        return false;
    });

    //添加表格类型信息
    form.on('submit(edit)', function(data){
        console.log(data);
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        //初始化数据
        $.ajax({
            url: '/table/add',
            data: data.field,
            type: 'POST',
            success: function (data) {
                if(data.code == 200) {
                    top.layer.close(index);
                    top.layer.msg("添加成功！");
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                } else {
                    top.layer.close(index);
                    top.layer.msg("添加失败！");
                }
            },
            error:function () {
                top.layer.close(index);
                top.layer.msg("添加失败！");
            }
        });
        return false;
    });

    /**
     * 获取路径中的参数
     * @returns {Object}
     */
    function GetRequest() {
        var url = location.search;         //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            var strs = str.split("&");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

});

